<template>
  <div class="login">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {postJson} from "@/common/js/axios"
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        token: ''
      }
    }
  },
  methods: {
    onSubmit() {
      postJson("/token/login",this.form,0).then(res => {
        console.log(res);
        if ( 'code' in res ) {
          // 将对象转为json
          this.form.token = res.data
          sessionStorage.setItem('user',JSON.stringify(this.form))
          // 从哪来回哪去
          if (this.$route.query.redirect) {
            // 跳转到原页面
            this.$router.push({path: decodeURIComponent(this.$route.query.redirect)})
          } else {
            this.$router.push("/") //正常登录流程进入的页面
          }
        }
      })
      
      
    }
  }
}
</script>

<style>
.login {
  width: 400px;
}
</style>